package com.xmu.edu.tisp.contract.plantapplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xmu.edu.tisp.R;

public class Selected_FieldAdapter extends BaseAdapter{
	private Context context;
	private List<HashMap<String, String>> list;
	private ArrayList<String> deletedlist = new ArrayList<String>();
	private String FARMER_CD;
    public Selected_FieldAdapter(Context context,List<HashMap<String, String>> list , String farmer_cd){
    	super();
    	this.context=context;
		this.list = list;
		this.FARMER_CD = farmer_cd;
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int index=position;
		View view=convertView;
		if(view==null){
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    view = inflater.inflate(R.layout.selected_fielditem, null);
		 
		}
		HashMap<String, String> map = list.get(position);
		TextView textView=(TextView) view.findViewById(R.id.SFI_Item_field_cd);
		
		textView.setText( map.get("FIELD_BLOCK_CODE"));
		
		
		TextView textView1 = (TextView) view.findViewById(R.id.SFI_Item_field_name);
		textView1.setText("("+map.get("FIELD_MANAGER")+")");
		
		ImageButton delete = (ImageButton) view.findViewById(R.id.field_delete);
		delete.setTag(position);
		delete.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				delete(index);
				deletedlist.add(list.get(index).get("FIELD_BLOCK_CODE"));
				list.remove(index);
				
				notifyDataSetChanged();
			}
			
		});
		
		
		return view;
	}
	public List<HashMap<String, String>> getList(){
		return list;
		
	}
	
	private void delete(int index){
		SharedPreferences sp = context.getSharedPreferences(FARMER_CD+"PA", Activity.MODE_APPEND);
		SharedPreferences.Editor editor=sp.edit();
		int count = sp.getInt("FIELD_BLOCK_NUMBER", 0);
		for(int i = 0; i<count ; ++i){
			if(sp.getString("XJSZ_ZYMD_ID"+i, "").equals("")){
				continue;
			}
			else{
				if(sp.getString("XJSZ_ZYMD_ID"+i, "").equals(list.get(index).get("FIELD_BLOCK_ID"))){
					editor.remove("XJSZ_ZYMD_ID" +i);
					editor.remove("XJSZ_ZYMD_ID"+i+"PZDM");
					break;
					
				}
			}
		}
		editor.commit();
		
		
	}
	public void refresh(){
		list.clear();
		notifyDataSetChanged();
	}
	public ArrayList<String> getDeletedList(){
		return deletedlist;
		
	}
	
	

}
